package com.example.demo.controller;

import com.example.demo.entity.Item;
import com.example.demo.service.ItemService;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description TODO
 * @Author lililiang
 * @Date 21/03/02 09:29
 * @Version 1.0
 **/
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/save")
    public void save(Item item) {
        itemService.save(item);
    }

    @RequestMapping("/deleteById")
    public void deleteById(String id) {
        itemService.deleteById(id);
    }

    @RequestMapping("/findAll")
    public void findAll() {
        List<Item> list = IteratorUtils.toList(itemService.findAll().iterator());
        for (Item item : list) {
            System.out.println(item);
        }
    }

    @RequestMapping("/deleteAll")
    public void deleteAll() {
        itemService.deleteAll();
    }

    @RequestMapping("/findAllDesc")
    public List<Item> findAllDesc() {
        // 根据价格排序
        return IteratorUtils.toList(itemService.findAll(Sort.by(Sort.Direction.DESC, "price")).iterator());
    }

    @RequestMapping("/findAllBetween")
    public List<Item> findAllBetween(Long pr1, Long pr2) {
        // 根据价格区间查询
        return itemService.findByPriceBetween(pr1, pr2);
    }

    @RequestMapping("/searchSimilar")
    public Page<Item> searchSimilar(Item item, String[] s, Pageable pageable) {
        return itemService.searchSimilar(item, s, pageable);
    }


}
